package com.xk.sport.backend.dao.repository;

import com.xk.sport.backend.dao.model.Task;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface TaskDao extends JpaRepository<Task, Long>, JpaSpecificationExecutor<Task> {

    @Query(value = "select * from task where startTime >= ?1 and startTime <= ?2", nativeQuery = true)
    List<Task> findAll(long startTime, long endTime);

    @Query(value = "select * from task where startTime >= ?1 and startTime <= ?2 and teacherId != 0", nativeQuery = true)
    List<Task> findByTeacherIdNotNull(long startTime, long endTime);

    @Query(value = "select * from task where startTime >= ?1 and startTime <= ?2 and teacherId = 0", nativeQuery = true)
    List<Task> findByTeacherIdIsNull(long startTime, long endTime);

    @Query(value = "select * from task where startTime >= ?1 and teacherId = 0", nativeQuery = true)
    List<Task> findValidTask(long now);

    @Query(value = "select * from task where startTime >= ?1 and teacherId = 0 and lessonId in (?2)", nativeQuery = true)
    List<Task> findValidTaskByLessonIds(long now, List<Long> lessonIds);

    @Transactional
    @Modifying
    @Query(value = "update task set teacherId = ?1, selectTime= ?2 where id = ?3", nativeQuery = true)
    void updateTeacherIdAndTime(long teacherId, long selectTime, long taskId);
}
